package de.chessgame.block;

import java.util.List;
import java.util.Random;

import org.lwjgl.input.Mouse;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import de.chessgame.ChessGameMod;
import de.chessgame.handler.ChessGameGuiHandler;
import de.chessgame.tileentity.TileEntityChessBoard;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityBrewingStand;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class BlockChessBoard extends BlockContainer {

	private IIcon iconChessGame;

	public BlockChessBoard() {
		super(Material.wood);
	}

	/**
	 * Is this block (a) opaque and (b) a full 1m cube? This determines whether
	 * or not to render the shared face of two adjacent blocks and also whether
	 * the player can attach torches, redstone wire, etc to this block.
	 */
	public boolean isOpaqueCube() {
		return false;
	}

	/**
	 * The type of render function that is called for this block
	 */
	public int getRenderType() {
		return -1;
	}

	@Override
	public boolean canPlaceBlockOnSide(World p_149707_1_, int p_149707_2_,
			int p_149707_3_, int p_149707_4_, int side) {
		return super.canPlaceBlockOnSide(p_149707_1_, p_149707_2_, p_149707_3_,
				p_149707_4_, side) && side == 1;
	}

	@Override
	public boolean canPlaceTorchOnTop(World world, int x, int y, int z) {
		return false;
	}

	@Override
	public boolean canConnectRedstone(IBlockAccess world, int x, int y, int z,
			int side) {
		return false;
	}

	@Override
	public boolean canBeReplacedByLeaves(IBlockAccess world, int x, int y, int z) {
		return false;
	}

	/**
	 * Returns a new instance of a block's tile entity class. Called on placing
	 * the block.
	 */
	public TileEntity createNewTileEntity(World world, int dontKnow) {
		return new TileEntityChessBoard();
	}

	/**
	 * If this block doesn't render as an ordinary block it will return False
	 * (examples: signs, buttons, stairs, etc)
	 */
	public boolean renderAsNormalBlock() {
		return false;
	}

	/**
	 * Adds all intersecting collision boxes to a list. (Be sure to only add
	 * boxes to the list if they intersect the mask.) Parameters: World, X, Y,
	 * Z, mask, list, colliding entity
	 */
	public void addCollisionBoxesToList(World world, int x, int y, int z,
			AxisAlignedBB axis, List list, Entity entity) {
		this.setBlockBounds(0.0F, 0.0F, 0.0F, 1f, 0.03125f, 1f);
		super.addCollisionBoxesToList(world, x, y, z, axis, list, entity);
		this.setBlockBoundsForItemRender();
		super.addCollisionBoxesToList(world, x, y, z, axis, list, entity);
	}

	/**
	 * Sets the block's bounds for rendering it as an item
	 */
	public void setBlockBoundsForItemRender() {
		this.setBlockBounds(0.0F, 0.0F, 0.0F, 1f, 0.5f, 1f);
	}
	
	/**
	 * Called upon block activation (right click on the block.)
	 */
	public boolean onBlockActivated(World world, int x, int y, int z,
			EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
		/*
		 * if (world.isRemote) { return false; } else {
		 */
		TileEntityChessBoard tileEntityChessTable = (TileEntityChessBoard) world
				.getTileEntity(x, y, z);
		
		if (tileEntityChessTable.isSlotEmptyForPlayer(player)) {
			tileEntityChessTable.onPlayerClick(player);
		}
		

		if (tileEntityChessTable != null && !player.isSneaking()
				&& tileEntityChessTable.isUseableByPlayer(player)) {
			player.openGui(ChessGameMod.instance, ChessGameGuiHandler.CHESS_BOARD, world, x, y, z);
		}

		return true;/*
					 * }
					 */
	}

	public void breakBlock(World world, int x, int y, int z, Block block,
			int p_149749_6_) {
		super.breakBlock(world, x, y, z, block, p_149749_6_);
	}

	public Item getItemDropped(int p_149650_1_, Random p_149650_2_,
			int p_149650_3_) {
		return ChessGameMod.itemChessBoard;
	}

	/**
	 * Gets an item for the block being called on. Args: world, x, y, z
	 */
	@SideOnly(Side.CLIENT)
	public Item getItem(World world, int x, int y, int z) {
		return ChessGameMod.itemChessBoard;
	}

	/**
	 * If this returns true, then comparators facing away from this block will
	 * use the value from getComparatorInputOverride instead of the actual
	 * redstone signal strength.
	 */
	public boolean hasComparatorInputOverride() {
		return true;
	}

	@SideOnly(Side.CLIENT)
	public void registerBlockIcons(IIconRegister iconRegister) {
		super.registerBlockIcons(iconRegister);
		this.iconChessGame = iconRegister.registerIcon(ChessGameMod.MODID + ":"
				+ "itemchessboard");
	}

	@SideOnly(Side.CLIENT)
	public IIcon getIconChessGame() {
		return this.iconChessGame;
	}

	@Override
	public void onBlockPlacedBy(World world, int x, int y, int z,
			EntityLivingBase entity, ItemStack itemStack) {

		int dir = MathHelper
				.floor_double((double) (entity.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

		// world.setBlockMetadataWithNotify(x, y, z, l, 2);
		if (dir == 0) {
			world.setBlockMetadataWithNotify(x, y, z, 2, 2);
		}

		if (dir == 1) {
			world.setBlockMetadataWithNotify(x, y, z, 5, 2);
		}

		if (dir == 2) {
			world.setBlockMetadataWithNotify(x, y, z, 3, 2);
		}

		if (dir == 3) {
			world.setBlockMetadataWithNotify(x, y, z, 4, 2);
		}
	}

	public static int metadataToDirection(int metadata) {
		switch (metadata) {
		case 2:
			return 0;

		case 5:
			return 1;

		case 3:
			return 2;

		case 4:
			return 3;

		default:
			return -1;
		}
	}
}